package sz.szsmk.citizencard.ui.adapter;

import android.content.Context;
import android.os.Bundle;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.layout.DefaultLayoutHelper;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.UserCardItemBinding;
import sz.szsmk.citizencard.model.network.bean.response.HomeWhxfRes;
import sz.szsmk.citizencard.model.network.bean.response.QueryPageResourceRes;
import sz.szsmk.citizencard.model.network.bean.response.UserCardFavResponse;
import sz.szsmk.citizencard.util.ACache;
import sz.szsmk.citizencard.util.Constant;
import sz.szsmk.citizencard.util.JumpUtils;
import sz.szsmk.citizencard.util.SharedPreferencesTool;
import sz.szsmk.citizencard.util.StringUtils;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/7/9 11:41
 * description:
 */
public class UserCardViewAdapter extends DelegateAdapter.Adapter<UserCardViewAdapter.UserCardHolder> {


    private Context mContext;
    private LayoutHelper mGridLayoutHelper;
    private List<UserCardFavResponse.DataBean> mList = new ArrayList<>();
    private String dayByDay;
    private List<HomeWhxfRes.CultureInforsBean> whxfData;
    private List<QueryPageResourceRes.ResourceListBean> adaverData;

    private List<View> viewList = new ArrayList<View>();
    private String extent;


    public UserCardViewAdapter(Context context, DefaultLayoutHelper cardHelper) {
        mGridLayoutHelper = cardHelper;
        mContext = context;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mGridLayoutHelper;
    }

    @Override
    public int getItemViewType(int position) {
        return R.layout.user_card_item;
    }


    @Override
    public UserCardHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return UserCardHolder.creat(parent);
    }

    @Override
    public void onBindViewHolder(UserCardHolder holder, int position) {
        loadItemData(position, holder.binding);
    }

    private void loadItemData(int position, final UserCardItemBinding binding) {

        if (adaverData == null || adaverData.size() == 0) {
            binding.homeAdaver.setVisibility(View.GONE);
        } else {
            binding.homeAdaver.setVisibility(View.VISIBLE);
            final String resource_url = adaverData.get(0).getResource_url();
            if (resource_url != null) {
                Glide.with(mContext)
                        .load(resource_url)
                        .into(binding.homeAdaver);
            }
            int size = adaverData.size();
            if (size == 5) {
                binding.userWhxfAdd.setVisibility(View.GONE);
            }

            final String link_url = adaverData.get(0).getLink_url();
            if (link_url != null) {
                binding.homeAdaver.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (!TextUtils.isEmpty(link_url)) {
                            Bundle bundle = new Bundle();
                            bundle.putString("url", link_url);
                            bundle.putString(Constant.TITLE, "广告");
                            JumpUtils.jumptoNatvie(mContext, "webView", 0, bundle);
                        }
                    }
                });
            }
        }

        int cardCount = whxfData == null ? 0 : whxfData.size();

        String zzk = SharedPreferencesTool.getString(mContext, "zzk", "0");
        String xxnk = SharedPreferencesTool.getString(mContext, "xxnk", "0");
        final String access_token = ACache.get(mContext).getAsString("access_token");
        viewList.clear();

        for (int i = 0; i < cardCount; i++) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.user_vp_view, null, false);
            viewList.add(view);
        }
        binding.userWhxfAdd.setVisibility(View.GONE);
        binding.userWhxfAdd.setVisibility(cardCount < 5 ? View.VISIBLE : View.GONE);
        binding.userWhxfAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String mextend = ACache.get(DaggerApplication.getIns()).getAsString("mextend");
                String url = "http://whxf.szsmk.com/culture/add_card.html?" + mextend;
                Bundle bundle = new Bundle();
//                bundle.putString(Constant.TITLE, "文化消费");
                bundle.putString("url", url);
                JumpUtils.jumptoNatvie(mContext, "webView", 0, bundle);
            }
        });

        binding.userWhxfMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url = "http://whxf.szsmk.com/culture/home.html?" + extent;
                Bundle bundle = new Bundle();
//                bundle.putString(Constant.TITLE, "文化消费");
                bundle.putString("url", url);
                JumpUtils.jumptoNatvie(mContext, "webView", 0, bundle);
            }
        });

        binding.cardText.setText("您已经绑定" + cardCount + "张A卡，还可以绑定" + (5 - cardCount) + "张！");
        if (whxfData != null) {
            if (whxfData.size() == 0) {
                binding.whxfZhanwei.setVisibility(View.GONE);
                binding.userCardVp.setVisibility(View.GONE);
//                binding.whxfZhanwei.setVisibility(View.VISIBLE);
//                binding.userCardVp.setVisibility(View.GONE);
            } else {
                binding.whxfZhanwei.setVisibility(View.GONE);
                binding.userCardVp.setVisibility(View.GONE);
//                binding.whxfZhanwei.setVisibility(View.GONE);
//                binding.userCardVp.setVisibility(View.VISIBLE);
            }
        } else {
            binding.whxfZhanwei.setVisibility(View.GONE);
            binding.userCardVp.setVisibility(View.GONE);
//            binding.whxfZhanwei.setVisibility(View.VISIBLE);
//            binding.userCardVp.setVisibility(View.GONE);
        }
        MyPagerAdapter vpAdapter = new MyPagerAdapter(whxfData);
        binding.userCardVp.setAdapter(vpAdapter);
        binding.userCardVp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                setItemCardInfo(whxfData.get(position), binding);
            }

            @Override
            public void onPageSelected(int position) {
                try {
//                    setItemCardInfo(whxfData.get(position), binding);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


        if (zzk.equals("0")) {
            binding.userToaddWhxfk.setVisibility(View.GONE);
        } else {
            binding.userToaddWhxfk.setVisibility(View.GONE);
        }
//        if (zzk.equals("0")) {
//            binding.userToaddWhxfk.setVisibility(View.GONE);
//        } else {
//            binding.userToaddWhxfk.setVisibility(View.VISIBLE);
//        }
        if (xxnk.equals("0")) {
            binding.userToaddXxnk.setVisibility(View.GONE);
            binding.userCard.setVisibility(View.GONE);
        } else {
            binding.userToaddXxnk.setVisibility(View.GONE);

        }
        if (zzk.equals("0") && xxnk.equals("0")) {
            binding.userUncardIv.setVisibility(View.GONE);
        } else {
            binding.userUncardIv.setVisibility(View.GONE);
        }
        if (mList.size() == 0) {
            binding.userDesLl.setVisibility(View.GONE);

            binding.userCard.setVisibility(View.GONE);
        } else {

            binding.userCard.setVisibility(View.GONE);
            if (xxnk.equals("0")) {
                binding.userCard.setVisibility(View.GONE);
            } else {
                binding.userCard.setVisibility(View.GONE);
            }
            binding.userDesLl.setVisibility(View.GONE);
            final UserCardFavResponse.DataBean dataBean = mList.get(0);
            binding.userCardNum.setText(dataBean.getCardNo());


            double balance = (double) dataBean.getCardBalance();
            double mBalance = balance / 100;
            String string = StringUtils.doubleToString(mBalance);
//            final String string = doubleToString(mBalance);


            binding.userCardBalance.setText(string + "元");

//            binding.userCardBalance.setText(dataBean.getCardBalance() + "");
            binding.userCardTimes.setText(dataBean.getSpareTimes() + "");
            String endTime = dataBean.getEndTime();


            if (dataBean.getStateName() == null) {
                binding.userWkt.setText("未开通");
            }
            binding.userWkt.setText(dataBean.getStateName());

            binding.userCardEndtime.setText(dayByDay);
            binding.userCardEndtime.setText(dataBean.getEndTime());
            binding.userCardChange.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Bundle bundle = new Bundle();
                    bundle.putString("cardNo", dataBean.getCardNo());
                    JumpUtils.jumptoNatvie(mContext, "useraddcard", 0, bundle);
                }
            });

        }
        binding.userAddMyCard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (access_token == null) {
                    toLogin();
                } else {
                    JumpUtils.jump(mContext, "addmycard", 0);
                }
            }
        });

        binding.userToaddXxnkTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (access_token == null) {
                    toLogin();
                } else {
                    JumpUtils.jump(mContext, "useraddcard", 0);
                }
            }
        });
        binding.userCardMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String access_token = ACache.get(mContext).getAsString("access_token");
                String url = Constant.BASE_URL + "app-front-end/#/card/main?access_token=" + access_token;
                Bundle bundle = new Bundle();
                bundle.putString("url", url);
                JumpUtils.jumptoNatvie(mContext, "webView", 0, bundle);
            }
        });

    }

    /**
     * 设置卡片内容
     *
     * @param binding
     */
    public void setItemCardInfo(HomeWhxfRes.CultureInforsBean cultureInforsBean, UserCardItemBinding binding) {
        binding.whxfJfye.setText(cultureInforsBean.getIntegration());
        binding.whxfZfbtq.setText(cultureInforsBean.getCouponcount());
        binding.whxfXfcs.setText(cultureInforsBean.getOrdercount());
    }

    public void toLogin() {
        JumpUtils.jump(mContext, "login", 0);
    }

    @Override
    public int getItemCount() {
        return 1;
    }

    public void setCardData(List<UserCardFavResponse.DataBean> beanList) {
        mList = beanList;
    }

    public void setWhxfData(List<HomeWhxfRes.CultureInforsBean> whxfData) {
        this.whxfData = whxfData;
    }

    public void setAdaverData(List<QueryPageResourceRes.ResourceListBean> adaverData) {
        this.adaverData = adaverData;
    }

    public void setExtent(String extent) {
        this.extent = extent;
    }

    static class UserCardHolder extends RecyclerView.ViewHolder {

        private UserCardItemBinding binding;

        public static UserCardHolder creat(ViewGroup parent) {
            UserCardItemBinding binding = UserCardItemBinding
                    .inflate(LayoutInflater.from(parent.getContext()), parent, false);
            return new UserCardHolder(binding);
        }


        public UserCardHolder(UserCardItemBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }


    private class MyPagerAdapter extends PagerAdapter {

        private List<HomeWhxfRes.CultureInforsBean> mList;

        MyPagerAdapter(List<HomeWhxfRes.CultureInforsBean> whxfData) {
            mList = whxfData;
        }

        @Override
        public int getCount() {
            return viewList == null ? 0 : viewList.size();
        }

        @Override
        public int getItemPosition(Object object) {
            return super.getItemPosition(object);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = viewList.get(position);

            TextView name = (TextView) view.findViewById(R.id.user_whxf_name);
            TextView whxfCard = (TextView) view.findViewById(R.id.user_whxf_id);
            TextView whxfTitle = (TextView) view.findViewById(R.id.user_whxf_title);
            if (mList != null && mList.size() > 0) {
                HomeWhxfRes.CultureInforsBean cultureInforsBean = mList.get(position);
                String cardNo = cultureInforsBean.getCardNo();

                String custName = cultureInforsBean.getCustName();
                String integration = cultureInforsBean.getIntegration();
                String ordercount = cultureInforsBean.getOrdercount();
//            String cardNo = whxfData.get(position).getCardNo();
                whxfCard.setText(cardNo);
                name.setText("持卡人：" + custName);
            } else {
                whxfTitle.setText("苏州文化消费卡\n品质苏州·美好生活");
                name.setVisibility(View.GONE);
                whxfCard.setVisibility(View.GONE);
            }

            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(viewList.get(position));
        }

    }
}
